package model

import "time"

type Account struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	WeekAnswerDateMap map[string][]*TimeSlot `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type TimeSlot struct {
	Id int64 `xorm:"pk autoincr"`
	AccountId int64
	WeekAnswerDate string
	StartTime string
	EndTime string
	AnswerSlot string `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

func QueryTimeSlot(aId int64)(*Account, error){

	var a Account

	var weekStrList = [7]string{"周日", "周一", "周二", "周三", "周四", "周五", "周六"}

	for _, wsl := range weekStrList{

		var ts []*TimeSlot
		x.Where("account_id = ?", aId).And("week_answer_date = ?", wsl).Find(&ts)

		for _, t := range ts{
			t.AnswerSlot = t.StartTime + " - " + t.EndTime
		}

		a.WeekAnswerDateMap[wsl] = ts
	}

	return &a, nil
}

func InsertTimeSlot(a *Account)(map[string]interface{}, error){
	for mk, wadmv := range a.WeekAnswerDateMap{
		for _, tsv := range wadmv{

			var ts TimeSlot
			ts.AccountId = a.Id
			ts.WeekAnswerDate = mk
			ts.StartTime = tsv.StartTime
			ts.EndTime = tsv.EndTime
			x.Insert(&ts)
		}
	}

	return map[string]interface{}{"code":200, "message":"成功"}, nil

}